<template>
  <div class="app-container">
    <div class="search-container">
      <div class="search-wrap">
        <div style="display:inline-block;">
          <label class="radio-label" style="padding-left:0;">源IP: </label>
          <el-input v-model="fieldName" placeholder="" style="width:240px;"/>
        </div>

        <div style="display:inline-block;">
          <label class="radio-label">目的IP: </label>
          <el-input v-model="fieldName" placeholder="" style="width:240px;"/>
        </div>
        <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="primary" icon="el-icon-search">
          搜索
        </el-button>
        <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="info" icon="el-icon-refresh-right">
          重置
        </el-button>
      </div>

      <div class="btn-wrap radio-label">
        <el-button :loading="downloadLoading" style="margin:0 0 20px 20px;" type="primary" icon="el-icon-circle-plus-outline"
        @click="dialogTableVisible = true">
          新增
        </el-button>
      </div>
    </div>

    <el-table v-loading="listLoading" :data="list" element-loading-text="Loading..."
      border fit highlight-current-row>
      <el-table-column align="center" label="编号" width="95">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="源IP">
        <template slot-scope="scope">
          {{ scope.row.sip }}
        </template>
      </el-table-column>
      <el-table-column label="目的IP">
        <template slot-scope="scope">
          {{ scope.row.dip }}
        </template>
      </el-table-column>
      <el-table-column label="路由跳数">
        <template slot-scope="scope">
          {{ scope.row.routeNum }}
        </template>
      </el-table-column>
      <el-table-column label="动态">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isolate == '否'">{{ scope.row.isolate }}</el-tag>
          <el-tag type="success" v-if="scope.row.isolate == '是'">{{ scope.row.isolate }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="变换周期">
        <template slot-scope="scope">
          {{ scope.row.change }}分钟
        </template>
      </el-table-column>

      <el-table-column label="操作" width="260" align="center">
        <template slot-scope="{row}">
          <el-button
            type="warning"
            size="mini"
            icon="el-icon-edit"
          >
            修改
          </el-button>

          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
          >
            删除
          </el-button>
        </template>
        
      </el-table-column>
    </el-table>

    <div class="pagination">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="1000">
      </el-pagination>
    </div>


    <el-dialog v-el-drag-dialog :visible.sync="dialogTableVisible" title="新增虚拟路径" @dragDialog="handleDrag" width="1200px">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="right" label-width="120px">
        <el-row>
          <el-col :span="24">
            <div class="form-title">路径配置</div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="源IP" prop="title">
              <el-input value="192.168.10.100"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="目的IP" prop="title">
              <el-input value="10.10.10.10"></el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="路由跳数" prop="title">
              <el-input value="3"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="动态" prop="title">
              <el-switch
                v-model="temp.isolate"
                active-text="是"
                inactive-text="否">
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="变换周期" prop="title">
              <el-input value="10">
                <template slot="append">分钟</template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <div class="form-title">
              路径节点
              <span style="margin-left: 5px;font-size: 12px;color: #ff4949">(多个使用;分隔)</span>
            </div>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-input
              type="textarea"
              :rows="4"
              v-model="ipListText">
            </el-input>
          </el-col>
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogTableVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogTableVisible = false">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList } from '@/api/article'
import { parseTime } from '@/utils'

import Pagination from '@/components/Pagination'
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'Host',
  components: { Pagination },
  directives: { elDragDialog },
  data() {
    return {
      list: null,
      fieldName: '',
      ipListText: '192.168.2.1;192.168.3.1;192.168.4.1;192.168.5.1',
      listLoading: true,
      downloadLoading: false,
      autoWidth: true,


      dialogTableVisible: false,

      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      temp: {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        type: '',
        status: 'published',
      },
      hostOptions: ['28.12.125.74','47.45.109.68','12.16.84.57','176.86.94.57','175.116.180.77','112.116.110.107','182.16.10.8','2.16.105.57','182.161.122.77'],
      typeOptions: ['外到内访问','内到外访问'],
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    flitterData(arr) {
      let spanOneArr = []
      let concatOne = 0
      arr.forEach((item, index) => {
          if (index === 0) {
              spanOneArr.push(1)
          } else {
              //realName 修改
              if (item.realName === arr[index - 1].v) {
                  //第一列需合并相同内容的判断条件
                  spanOneArr[concatOne] += 1
                  spanOneArr.push(0)
              }else {
                  spanOneArr.push(1)
                  concatOne = index
              }
          }
      })
      console.log("1",{
          one: spanOneArr,
      })
      return {
          one: spanOneArr,
      }
    },
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0) {
        const _row = this.flitterData(this.list).one[rowIndex]
        const _col = _row > 0 ? 1 : 0
        return {
          rowspan: _row,
          colspan: _col,
        }
      }
      
    },
    fetchData() {
      this.listLoading = true
      let data = []
      fetchList().then(response => {
        data.push({
          sip: '192.168.10.23',
          dip: '82.18.24.18',
          routeNum: 10,
          change: 15,
          isolate: '是'
        })
        data.push({
          sip: '12.18.19.63',
          dip: '101.76.54.218',
          routeNum: 12,
          change: 6,
          isolate: '否'
        })
        data.push({
          sip: '72.98.19.63',
          dip: '110.86.64.98',
          routeNum: 4,
          change: 22,
          isolate: '是'
        })
        data.push({
          sip: '82.45.52.22',
          dip: '10.56.14.85',
          routeNum: 7,
          change: 5,
          isolate: '否'
        })
        this.list = data
        this.listLoading = false
      })
    },
    handleDrag() {
      console.log("添加页面")
    }
  }
}
</script>

<style scoped>
.radio-label {
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 30px;
}
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: right;
}
.search-container {
  display: flex;
  justify-content: space-between;
}
.search-wrap {
  flex: 1;
}


.form-title {
  margin-bottom: 10px;
  padding-bottom: 10px;
  display: flex;
  align-items: center;
  font-size: 14px;
  font-weight: bold;
  border-bottom: 1px solid #F1F1F1;
  box-sizing: border-box;
}
.el-select {
  width: 100%;
}
</style>
